package cn.didi.feidi.androidorder.assembly;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import cn.didi.feidi.androidorder.util.AppUtil;

/**
 * Created by jinxiong on 2017/8/29.
 */

public abstract class BaseDialogFragment extends DialogFragment {

    protected int leftPadding = 20;//dp
    protected int rightPadding = 20;//dp
    protected int gravity = Gravity.CENTER_VERTICAL;


    protected abstract
    @NonNull
    Dialog newDialog();

    protected abstract void addListener();

    protected abstract void initView();

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = newDialog();
        setLocation(dialog);
        addListener();
        initView();
        return dialog;
    }

    /**
     * 设置位置
     *
     * @param dialog
     */
    protected void setLocation(Dialog dialog) {
        // 拿到Dialog的Window, 修改Window的属性
        Window window = dialog.getWindow();

        window.getDecorView().setPadding(AppUtil.dp2px(getActivity(), leftPadding), 0,
                AppUtil.dp2px(getActivity(), rightPadding), 0);

        // 获取Window的LayoutParams
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.gravity = gravity;
        // 一定要重新设置, 才能生效
        window.setAttributes(attributes);
    }

    /**
     * @param leftPadding dp
     */
    public BaseDialogFragment setLeftPadding(int leftPadding) {
        this.leftPadding = leftPadding;
        return this;
    }

    /**
     * @param rightPadding dp
     */
    public BaseDialogFragment setRightPadding(int rightPadding) {
        this.rightPadding = rightPadding;
        return this;
    }

    public BaseDialogFragment setDialogGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }
}
